/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.data.controller.AICtlr;
import org.gov.vis.data.controller.AICtlrImpl;
import org.gov.vis.data.dto.AIDto;
import org.gov.vis.data.gui.EditAIDialog;
import org.gov.vis.data.gui.ViewAIDialog;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class ViewAIListener implements ActionListener,BaseListener{





	private AICtlr aiCtlr = new AICtlrImpl();
	private ViewAIDialog viewAIDialog;
	private AIDto aiDto;
	final private ResourceBundle rb = Util.getResources();
	private EditAIDialog editAIDialog;

	public ViewAIListener(ViewAIDialog viewAIDialog) {
		// TODO Auto-generated constructor stub
		this.viewAIDialog = viewAIDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewAIDialog _viewAIDialog =(ViewAIDialog)_guiObject;
		aiDto = new AIDto();

		String code = _viewAIDialog.getCodeTxtBx().getText();
		if(code!=null){
			aiDto.setCode(code);
		}
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		AIDto _aiDto = (AIDto)_dtoObject;
		if(editAIDialog != null){

			editAIDialog.getTxtFldCode().setText(_aiDto.getCode());
			editAIDialog.getAnimalType().setSelectedItem(_aiDto.getAnimalType());
			editAIDialog.getTxtFldBrthGvn().setText(_aiDto.getBirthGiven());
			editAIDialog.getTxtFldBreed().setText(_aiDto.getBreed());
			editAIDialog.getTxtFldBullNo().setText(_aiDto.getBullNo());
			editAIDialog.getTxtFldCattleOWR().setText(_aiDto.getCattleOwner());
			editAIDialog.getDtChoDateOfInsemination().setDate(_aiDto.getDoi());
			editAIDialog.getTxtFldAIFee().setText(_aiDto.getFee());
			editAIDialog.getTxtFldKndOfAnml().setText(_aiDto.getKindOfAnimal());
			editAIDialog.getTxtFldMand().setText(_aiDto.getMandal());
			editAIDialog.getTxtFldNoAI().setText(_aiDto.getNoOfAI());
			editAIDialog.getTxtFldRecpNo().setText(_aiDto.getReceiptNo());
			//editAIDialog.getTxtFldTOI().setText(_aiDto.getToi());
			editAIDialog.getTxtFldVill().setText(_aiDto.getVillage());
			editAIDialog.setHidnObject(_aiDto.getAiId());
			editAIDialog.getCombxHours().setSelectedIndex(Integer.parseInt(_aiDto.getToi().getHour()));
			editAIDialog.getCombxMinutes().setSelectedIndex(Integer.parseInt(_aiDto.getToi().getMinute()));
			editAIDialog.getCombxMeridian().setSelectedItem(_aiDto.getToi().getMeridian());

		}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub


		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewAIDialog);
			try {

				List<AIDto> aiLst = aiCtlr.searchAIs(aiDto);

				List<String> colLst = new ArrayList<String>();
				colLst.add("Code");
				colLst.add("Date of Insiminaton");
				colLst.add("Breed");
				colLst.add("Bull No");
				colLst.add("Kind of Animal");

				ArrayList<Object> dataList = new ArrayList<Object>(1);
				if(aiLst != null && !aiLst.isEmpty()){
					for(AIDto dto:aiLst){
						ArrayList<Object> lst = new ArrayList<Object>(1);
						//setting each property of the current Accessory object (row) to the list
						lst.add(dto);
						lst.add(dto.getCode());
						lst.add(dto.getDoi());
						lst.add(dto.getBreed());
						lst.add(dto.getBullNo());
						lst.add(dto.getKindOfAnimal());
						dataList.add(lst);
					}//end for 
				}//end if
				VISDataGrid.createDataPanel(dataList, colLst, viewAIDialog.getPnlAIResults(),this);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


		if(actionEvent.getSource() instanceof VISHyperLink){
			actionEvent.getSource();
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			AIDto aiDto = (AIDto)visHyperLink.getObject();
			//System.out.println("action from the object----"+aiDto.getCode());
			editAIDialog = new EditAIDialog(Main.getMainFrame(),
					rb.getString("dialog_edit_ai_title"),
					false);
			setDtoDetailsToGui(aiDto);
			viewAIDialog.dispose();

		}

	}






}
